// 通过时间戳解析获取年、月、日、时、分、秒、星期信息
export const timestampToDate = (timestamp) => {
	let weeks = ['天', '一', '二', '三', '四', '五', '六'],
		time = timestamp.toString().length == 10 ? timestamp * 1000 : timestamp, //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		date = new Date(time),
		Y = date.getFullYear(),
		M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),
		D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
		W = date.getDay(),
		H = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
		I = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
		S = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	return {
		Y,
		M,
		D,
		H,
		I,
		S,
		W: weeks[W],
		H_I:H +":"+ I,
		H_I_S:H +":"+ I +":"+ S,
		Y_M_D: Y + '-' + M + '-' + D,
		Y_M_D_H_I: Y + '-' + M + '-' + D + " "+ H +":"+ I,
		Y_M_D_H_I_S: Y + '-' + M + '-' + D + " "+ H +":"+ I +":"+ S,
	};
};
// 通过日期时间格式转换为时间戳
export const dateToTimestamp = (date) => {
	let patt = /[^\d]*(\d{4})*[^\d]*(\d{1,2})*[^\d]*(\d{1,2})*[^\d]*(\d{2})*[^\d]*(\d{1,2})*[^\d]*(\d{1,2})*[^\d]*/;
	let dateSplitArr = date.match(patt);
	return new Date(dateSplitArr[1],dateSplitArr[2]-1,dateSplitArr[3]||"00",dateSplitArr[4]||"00",dateSplitArr[5]||"00",dateSplitArr[6]||"00").valueOf();
};
// 秒转换成时间格式
export const secondsToDate = (seconds) => {
	seconds = seconds >=0 ? seconds : 0;
	let S = parseInt(seconds%60);
	S = S > 9 ? S.toString() : "0" + S;
	let I = Math.floor(seconds%(60*60)/60);
	I = I > 9 ? I.toString() : "0" + I;
	// let H = Math.floor(seconds%(60*60*24)/(60*60));
	let H = Math.floor(seconds/(60*60));
	H = H > 9 ? H.toString() : "0" + H;
	return {
		H,
		I,
		S,
		H_I_S:H+":"+I+":"+S
	}
}
// 时间格式转换成秒
export const dateToSeconds = (H_I_S="") => {
	let [
		H=0,
		I=0,
		S=0,
	] = H_I_S.split(":");
	return parseFloat(H)*60*60 + parseFloat(I)*60 + parseFloat(S);
}